package com.commerce.service.impl;

import com.commerce.common.Reminder;
import com.commerce.mapping.MenuMapping;
import com.commerce.model.Menu;
import com.commerce.service.MenuService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author wyj
 * @Title:
 * @Description:
 */
@Service
public class MenuServiceImpl implements MenuService{


    @Autowired
    private MenuMapping menuMapping;
    /**
     * 查询所有
     *
     * @return
     */
    @Override
    public List<Menu> selectList() {
        return menuMapping.selectList();
    }

    /**
     * 新增
     *
     * @param menu
     * @return
     */
    @Override
    public String insert(Menu menu , HttpServletRequest request) {
        if(menu.getP_id() != 0){
            if(menuMapping.selectById(menu.getP_id()) == null){
                return Reminder.nopiderror.getName();
            }
        }
        return menuMapping.insert(menu)>0? Reminder.success.getName():Reminder.saveerror.getName();
    }

    /**
     * 修改
     *
     * @param menu
     * @return
     */
    @Override
    public String update(Menu menu, HttpServletRequest request) {
        if(menu.getP_id() != 0){
            if(menuMapping.selectById(menu.getP_id()) == null){
                return Reminder.nopiderror.getName();
            }
        }
        return menuMapping.update(menu)>0? Reminder.success.getName():Reminder.updateerror.getName();
    }

    /**
     * 根据ID删除
     *
     * @param id
     * @return
     */
    @Override
    public String deleteById(int id) {
        return menuMapping.delete(id)>0? Reminder.success.getName():Reminder.deleteerror.getName();
    }

    /**
     * 查询一级菜单
     *
     * @return
     */
    @Override
    public List<Menu> selectMainMenu() {
        return menuMapping.selectMainMenu();
    }

    /**
     * 查询单个数据
     *
     * @param id
     * @return
     */
    @Override
    public Menu selectById(int id) {
        return menuMapping.selectById(id);
    }
}
